﻿using Model;
using Model.Medicine;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace QlpmClient
{
    public class ConvertData
    {
        public DataTable ConverArrPatientToDatatable(Patient[] p)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("patientid", typeof(int));
            dt.Columns.Add("fullname", typeof(String));
            dt.Columns.Add("idcard", typeof(String));
            dt.Columns.Add("sex", typeof(String));
            dt.Columns.Add("dateofbirth", typeof(String));
            dt.Columns.Add("address", typeof(String));
            dt.Columns.Add("phonenumber", typeof(String));
            dt.Columns.Add("relative", typeof(String));
            DataRow dr;
            string sex = "";
            if (p != null)
            {
                for (int i = 0; i < p.Length; i++)
                {
                    dr = dt.NewRow();
                    dr["patientid"] = p[i].getPatientId();
                    dr["fullname"] = p[i].getFullName();
                    dr["idcard"] = p[i].getIdCard();
                    if (p[i].getSex() == Model.Person.Sex.MALE)
                        sex = "Nam";
                    else
                        if (p[i].getSex() == Model.Person.Sex.FEMALE)
                            sex = "Nữ";
                        else
                            sex = "Chưa cập nhật";
                    dr["sex"] = sex;
                    dr["dateofbirth"] = p[i].getDateOfBirth().toShortDateString();
                    dr["address"] = p[i].getAddress();
                    dr["phonenumber"] = p[i].getPhoneNumber();
                    dr["relative"] = p[i].getRelative();
                    dt.Rows.Add(dr);
                }

            }

            return dt;
        }



        public DataTable DataTablePatient(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("patientid", typeof(int));
            dt.Columns.Add("fullname", typeof(String));
            dt.Columns.Add("idcard", typeof(String));
            dt.Columns.Add("sex", typeof(String));
            dt.Columns.Add("dateofbirth", typeof(String));
            dt.Columns.Add("address", typeof(String));
            dt.Columns.Add("phonenumber", typeof(String));
            dt.Columns.Add("relative", typeof(String));
            string sex = "";
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["patientid"] = row[0];
                    dr["fullname"] = row[1];
                    dr["idcard"] = row[2];
                    if (row[3] == "1")
                        sex = "Nam";
                    else
                        if (row[3] == "2")
                            sex = "Nữ";
                        else
                            sex = "Chưa Xác Định";
                    dr["sex"] = sex;
                    dr["dateofbirth"] = row[4];
                    dr["address"] = row[5];
                    dr["phonenumber"] = row[6];
                    dr["relative"] = row[7];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable ConvertArrWorkerToDatatable(Worker[] w)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("workerid", typeof(int));
            dt.Columns.Add("fullname", typeof(String));
            dt.Columns.Add("idcard", typeof(String));
            dt.Columns.Add("sex", typeof(String));
            dt.Columns.Add("dateofbirth", typeof(String));
            dt.Columns.Add("address", typeof(String));
            dt.Columns.Add("phonenumber", typeof(String));
            dt.Columns.Add("positionid", typeof(String));
            dt.Columns.Add("username", typeof(String));
            DataRow dr;
            string sex = "";
            if (w != null)
            {
                for (int i = 0; i < w.Length; i++)
                {
                    dr = dt.NewRow();
                    dr["workerid"] = w[i].getWorkerId();
                    dr["fullname"] = w[i].getFullName();
                    dr["idcard"] = w[i].getIdCard();
                    if (w[i].getSex() == Model.Person.Sex.MALE)
                        sex = "Nam";
                    else
                        if (w[i].getSex() == Model.Person.Sex.FEMALE)
                            sex = "Nữ";
                        else
                            sex = "Chưa cập nhật";
                    dr["sex"] = sex;
                    dr["dateofbirth"] = w[i].getDateOfBirth().toShortDateString();
                    dr["address"] = w[i].getAddress();
                    dr["phonenumber"] = w[i].getPhoneNumber();
                    if (w[i].getPositionId() == 1)
                        dr["positionid"] = "Bác Sỹ";
                    else
                        dr["positionid"] = "Nhân Viên";
                    dr["username"] = w[i].getUsername();
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        public DataTable ConvertArrWorkerToDatatableWorkerSearch(Worker[] w)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("workerid", typeof(int));
            dt.Columns.Add("fullname", typeof(String));
            dt.Columns.Add("dateofbirth", typeof(String));
            DataRow dr;
            if (w != null)
            {
                for (int i = 0; i < w.Length; i++)
                {
                    dr = dt.NewRow();
                    dr["workerid"] = w[i].getWorkerId();
                    dr["fullname"] = w[i].getFullName();
                    dr["dateofbirth"] = w[i].getDateOfBirth().toShortDateString();
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }

        public DataTable ConvertArrPositionToDatatable(Position[] p)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("positionid", typeof(int));
            dt.Columns.Add("positonname", typeof(String));
            DataRow dr;

            if (p != null)
            {
                for (int i = 0; i < p.Length; i++)
                {

                    dr = dt.NewRow();
                    dr["positionid"] = p[i].getPositionId();
                    dr["positonname"] = p[i].getPositionName();
                    dt.Rows.Add(dr);

                }
            }
            return dt;
        }

        public DataTable DataTableDIAGNOSIS(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("diagnosisid", typeof(int));
            dt.Columns.Add("diagnosisname", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["diagnosisid"] = row[0];
                    dr["diagnosisname"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableWorker(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("workerid", typeof(int));
            dt.Columns.Add("fullname", typeof(String));
            dt.Columns.Add("idcard", typeof(int));
            dt.Columns.Add("sex", typeof(String));
            dt.Columns.Add("dateofbirth", typeof(String));
            dt.Columns.Add("address", typeof(String));
            dt.Columns.Add("phonenumber", typeof(String));
            dt.Columns.Add("positionname", typeof(String));
            dt.Columns.Add("username", typeof(String));
           
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();

                    string sex;

                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["workerid"] = row[0];
                    dr["fullname"] = row[1];
                    dr["idcard"] = row[3];
                    if (row[4] == "1")
                        sex = "Nam";
                    else
                        if (row[3] == "2")
                            sex = "Nữ";
                        else
                            sex = "Chưa Xác Định";
                    dr["sex"] = sex;
                    dr["dateofbirth"] = row[2];
                    dr["address"] = row[5];
                    dr["phonenumber"] = row[6];
                    dr["positionname"] = row[7];
                    dr["username"] = row[8];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableThuocVatTu(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicineid", typeof(int));
            dt.Columns.Add("medicinename", typeof(String));
            dt.Columns.Add("medicinestrength", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["medicineid"] = row[0];
                    dr["medicinename"] = row[1];
                    dr["medicinestrength"] = row[2];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }


        public DataTable DataTablerouteofadministration(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("routeofadminid", typeof(int));
            dt.Columns.Add("routeofadminname", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["routeofadminid"] = row[0];
                    dr["routeofadminname"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableAdvice(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("adviceid", typeof(int));
            dt.Columns.Add("advicename", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["adviceid"] = row[0];
                    dr["advicename"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableSymptom(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("symptomid", typeof(int));
            dt.Columns.Add("symptomname", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["symptomid"] = row[0];
                    dr["symptomname"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableMedicine(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicineid", typeof(int));
            dt.Columns.Add("medicinename", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["medicineid"] = row[0];
                    dr["medicinename"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable DataTableMedicineGroup(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicinegroupid", typeof(int));
            dt.Columns.Add("medicinegroupname", typeof(String));
            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["medicinegroupid"] = row[0];
                    dr["medicinegroupname"] = row[1];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }


        public DataTable ConvertArrMedUnitToDatatable(MedUnit[] m)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicineunitid", typeof(int));
            dt.Columns.Add("medicineunitname", typeof(String));
            DataRow dr;

            if (m != null)
            {
                for (int i = 0; i < m.Length; i++)
                {

                    dr = dt.NewRow();
                    dr["medicineunitid"] = m[i].getMedUnitId();
                    dr["medicineunitname"] = m[i].getMedUnitName();
                    dt.Rows.Add(dr);

                }
            }
            return dt;
        }
        public DataTable ConvertArrMedicineToDatatable(string[] str)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicineid", typeof(int));
            dt.Columns.Add("medicinename", typeof(String));
            dt.Columns.Add("medicinestrength", typeof(String));
            dt.Columns.Add("routeofadminname", typeof(String));
            dt.Columns.Add("medicineunitname", typeof(String));
            dt.Columns.Add("medicinegroupname", typeof(String));
            dt.Columns.Add("quantitytotal", typeof(String));

            DataRow dr;
            if (str != null)
            {
                for (int i = 0; i < str.Length; i++)
                {
                    dr = dt.NewRow();
                    string[] row = str[i].Split(new string[] { "<<_>>" }, StringSplitOptions.None);
                    dr["medicineid"] = row[0];
                    dr["medicinename"] = row[1];
                    dr["medicinestrength"] = row[2];
                    dr["routeofadminname"] = row[3];
                    dr["medicineunitname"] = row[4];
                    dr["medicinegroupname"] = row[5];
                    dr["quantitytotal"] = row[6];
                    dt.Rows.Add(dr);
                }
            }

            return dt;
        }

        public DataTable ConvertArrMedGroupToDatatable(MedGroup[] m)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("medicinegroupid", typeof(int));
            dt.Columns.Add("medicinegroupname", typeof(String));
            DataRow dr;

            if (m != null)
            {
                for (int i = 0; i < m.Length; i++)
                {

                    dr = dt.NewRow();
                    dr["medicinegroupid"] = m[i].getMedGroupId();
                    dr["medicinegroupname"] = m[i].getMedGroupName();
                    dt.Rows.Add(dr);

                }
            }
            return dt;
        }

    }
}
